package com.supr.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.supr.model.Category;
import com.supr.model.Friend;
import com.supr.model.Manager;
import com.supr.model.Result;
import com.supr.model.Tag;
import com.supr.service.ArticleService;
import com.supr.service.CategoryService;
import com.supr.service.FriendService;
import com.supr.service.TagService;
import com.supr.util.Constant;
import com.supr.util.JsonUtil;
import com.supr.util.Pager;

@Controller
public class CategoryController {
    
	@Autowired
	private CategoryService categoryService;

	@Autowired
	private TagService tagService;

	@Autowired
	private FriendService friendService;

	/**
	 * 跳转列表
	 */
	@RequestMapping(value = "/admin/category/list")
	public String list(HttpSession session, ModelMap map) {
		Manager manager = (Manager) session.getAttribute(Constant.USERINFO);
		map.put("manager", manager);
		return "/admin/category/category_list";
	}

	/**
	 * 跳转新增页面
	 */
	@RequestMapping(value = "/admin/category/addJump")
	public String addJump(HttpSession session, ModelMap map) {
		Manager manager = (Manager) session.getAttribute(Constant.USERINFO);
		map.put("manager", manager);
		return "/admin/category/add_category";
	}

	/**
	 * 添加
	 */
	@RequestMapping(value = "/admin/category/add")
	public @ResponseBody Result addCategory(Category category, HttpSession session)
			throws UnsupportedEncodingException {
		System.out.println(category);
		category.setCategoryName(URLDecoder.decode(category.getCategoryName(), "utf-8"));
		category.setIconClass(URLDecoder.decode(category.getIconClass(), "utf-8"));
		category.setAliasName(URLDecoder.decode(category.getAliasName(), "uft-8"));
		if (categoryService.addCategory(category) > 0) {
			return new Result("success", Constant.DEAL_SUCCESS);
		} else {
			return new Result("fail", Constant.DEAL_FAIL);
		}
	}

	/**
	 * 跳转编辑页面
	 */
	@RequestMapping(value = "/admin/category/editJump/{id}")
	public String editJump(HttpSession session, ModelMap map, @PathVariable String id) {
		Category category = categoryService.getCategoryById(id);
		map.put("category", category);
		map.put("categoryId", id);
		return "/admin/category/edit_category";
	}

	/**
	 * 编辑
	 */
	@RequestMapping(value = "/admin/category/edit")
	public @ResponseBody Result editCategory(Category category, HttpSession session)
			throws UnsupportedEncodingException {
		category.setCategoryName(URLDecoder.decode(category.getCategoryName(), "utf-8"));
		if (categoryService.editCategory(category) > 0) {
			return new Result("success", Constant.DEAL_SUCCESS);
		} else {
			return new Result("fail", Constant.DEAL_FAIL);
		}
	}

	/**
	 * 获取分页列表
	 */
	@RequestMapping(value = "/admin/category/load")
	public String loadCategoryPageList(HttpSession session, ModelMap map, String param, Pager<Category> pager)
			throws UnsupportedEncodingException {
		Manager manager = (Manager) session.getAttribute(Constant.USERINFO);
		map.put("manager", manager);

		HashMap<String, Object> paramMap = new HashMap<String, Object>();

		Category category = JsonUtil.fromJson(param, Category.class);
		if (!StringUtils.isEmpty(category.getCategoryName())) {
			category.setCategoryName(URLDecoder.decode(category.getCategoryName(), "utf-8"));
		}
		// if(!StringUtils.isEmpty(category.getIconClass())){
		// category.setIconClass(URLDecoder.decode(category.getIconClass(),
		// "utf-8"));
		// }
		paramMap.put("category", category);
		// List<Category> categoryList = categoryService.getCategoryList();
		List<Category> categoryList = categoryService.getCategoryList(paramMap, pager);
		map.put("categoryList", categoryList);
		map.put("pager", pager);
		return "/admin/category/category_pager_list";
	}

	/**
	 * 获取文章分页列表
	 */
	@RequestMapping(value = "/category/{aliasName}")
	public String loadCategoryPage(HttpSession session, ModelMap map, @PathVariable String aliasName, String articleId,
			String categoryId) throws UnsupportedEncodingException {
		aliasName = URLDecoder.decode(aliasName, "utf-8");
		Category category = categoryService.getCategoryByAlias(aliasName);

		// 栏目列表
		List<Category> categoryList = categoryService.getCategoryList();

		// 标签列表
		List<Tag> tagList = tagService.getTagList();

		// 合作伙伴列表
		List<Friend> friendList = friendService.getAllFriendList();

		map.put("categoryList", categoryList);
		map.put("ca", category);
		map.put("tagList", tagList);
		map.put("friendList", friendList);

		return "/blog/category/article_pager";
	}

}
